package com.stable.actor.demo;

import com.stable.actor.core.ActorRef;
import com.stable.actor.core.ActorSystem;
import com.stable.actor.core.Props;

/**
 * 稳定的Actor框架Demo - 完全替换Akka
 */
public class StableActorDemo {

    public static void main(String[] args) throws InterruptedException {
        System.out.println("🚀 启动稳定的Actor框架Demo...");

        // 1. 创建Actor系统
        ActorSystem system = new ActorSystem("StableDemoSystem");

        try {
            // 2. 创建Master Actor
            ActorRef master = system.actorOf(Props.create(MasterActor.class), "master");
            System.out.println("✅ Master创建: " + master);

            // 3. 创建多个Client Actor
            String[] clientTypes = {"worker", "api", "scheduler", "monitor"};
            ActorRef[] clients = new ActorRef[4];
            for (int i = 0; i < clientTypes.length; i++) {
                String clientId = "client-" + (i + 1);
                String clientType = clientTypes[i];

                // 创建Client Actor
                ClientActor clientActor = new ClientActor(clientId, clientType);
                ActorRef clientRef = system.actorOf(Props.create(() -> clientActor), clientId);
                clientActor.setMaster(master);
                clients[i] = clientRef;
                System.out.println("✅ Client创建: " + clientRef + " (" + clientType + ")");

                // 间隔启动
                Thread.sleep(1000);
            }

            // 4. 运行一段时间
            System.out.println("\n⏳ 系统运行中...");
            Thread.sleep(5000); // 运行5秒

            // 5. 发送一些直接消息测试
            System.out.println("\n📤 发送测试消息...");
            for (int i = 0; i < clients.length; i++) {
//                master.tell("测试消息 " + i, ActorRef.noSender());
                master.tell("测试消息 " + i, clients[i]);
                Thread.sleep(1000);
            }

            Thread.sleep(5000);

        } finally {
            // 6. 关闭系统
            System.out.println("\n🛑 关闭Actor系统...");
            system.terminate();
        }

        System.out.println("🎉 稳定的Actor框架Demo完成!");
    }
}
